//
//  VisitorView.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/19.
//  Copyright © 2016年 young4ever. All rights reserved.
//

import UIKit

// swift宏定义协议
protocol VisitorViewDelegate: NSObjectProtocol{

    // 登录回调
    func loginButtonClicked()
    // 注册回调
    func registeButtonClicked()
}

class VisitorView: UIView {
    
    // 定义代理属性<必须加上weak, 防止循环引用>
    weak var delegate: VisitorViewDelegate?
    
    /**
     设置未登录界面是否是首页
     
     - parameter isHome:    是否是首页
     - parameter imageName: 展示图片名称
     - parameter message:   展示文本信息
     */
    func setupVisitorInfo(isHome: Bool, imageName: String, message: String) -> Void {
        
        // 如果是首页,隐藏转盘
        iconView.hidden = !isHome
        // 替换中间图片
        homeIcon.image = UIImage(named: imageName)
        // 替换文本
        msgLabel.text = message
        // 是否开启动画
        if isHome {
            startAnimation()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加子控件
        addSubview(iconView)
        addSubview(maskImage)
        addSubview(homeIcon)
        addSubview(msgLabel)
        addSubview(loginButton)
        addSubview(registeButton)
        
        // 子控件布局
        // 1.设置圆盘背景
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        // 2.maskImage
        maskImage.xmg_Fill(self)
        
        // 3.homeIcon
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        // 4.msgLabel
        msgLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        // "哪个控件" 的 "什么属性" "等于<大于,小于>" "另外一个控件" 的 "什么属性" 乘以 "多少" 加上 "多少"
        let widthConstraint = NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthConstraint)
        
        // 5.registeButton
        registeButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: msgLabel, size: CGSize(width: 100,height: 30), offset: CGPoint(x: 0, y: 20))
        
        // 6.loginButton
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: msgLabel, size: CGSize(width: 100,height: 30), offset: CGPoint(x: 0, y: 20))
    }
    
    // swift推荐我们自定义一个控件 , 要么使用纯代码 , 要么使用xib, 不能两个都写
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 按钮点击事件
    func registeButtonAction() -> Void {
        delegate?.registeButtonClicked()
    }
    
    func loginButtonAction() -> Void {
        delegate?.loginButtonClicked()
    }
    
    // MARK: - 首页转盘动画
    private func startAnimation(){
    
        // 1,创建动画
        let animation = CABasicAnimation()
        // 2,设置动画属性
        animation.keyPath = "transform.rotation"
        animation.repeatCount = MAXFLOAT
        animation.toValue = M_PI * 2
        animation.duration = 5.0
        animation.removedOnCompletion = false
        // 3,将动画添加到图层上
        iconView.layer.addAnimation(animation, forKey: "animation")
    }
    
    // MARK: - 懒加载控件
        /// 转盘
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
        /// 模糊的蒙版
    private lazy var maskImage: UIImageView = {
    
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()
    
        /// 图标
    private lazy var homeIcon: UIImageView = {
    
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return icon
    }()

        /// 文本
    private lazy var msgLabel: UILabel = {
    
        let label = UILabel()
        label.text = "swift推荐我们自定义一个控件 , 要么使用纯代码 , 要么使用xib, 不能两个都写"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
        /// 登录按钮
    private lazy var loginButton: UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VisitorView.loginButtonAction), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
        /// 注册按钮
    private lazy var registeButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VisitorView.registeButtonAction), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
